package com.taotao.adapter.controller;

import com.taotao.adapter.controller.impl.*;
import org.springframework.web.servlet.HandlerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wangjin
 * @CreateTime: 2022-06-27  21:10
 */
public class DispatchServlet {
    private List<ControllerAdapter> controllerAdapterList = new ArrayList<>();

    public DispatchServlet() {
        controllerAdapterList.add(new AnnotationControllerAdapter());
        controllerAdapterList.add(new ImplControllerAdapter());
        controllerAdapterList.add(new RequestHandlerControllerAdapter());

    }

        public void doDispatch(ImplController implController) {

        ControllerAdapter handlerAdapter = getHandler(implController);
        if (handlerAdapter == null) {
            return;
        }
        handlerAdapter.handler(implController);
    }

    public ControllerAdapter getHandler(Controller controller) {
        for (ControllerAdapter handlerAdapter :
                controllerAdapterList) {
            if (handlerAdapter.supports(controller)) {
                return handlerAdapter;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        new DispatchServlet().doDispatch(new ImplController());
    }
}